package threadlocal;

/**
 * 描述：演示ThreadLocal NPE
 */
public class ThreadLocalNPE {
    ThreadLocal<Long> longThreadLocal = new ThreadLocal<Long>();
    public void set () {
        longThreadLocal.set(Thread.currentThread().getId());
    }
    // 拆箱时会发生空指针异常 需要将long改为Long
    public long get() {
        return longThreadLocal.get();
    }

    public static void main(String[] args) {
        ThreadLocalNPE threadLocalNPE = new ThreadLocalNPE();
        System.out.println(threadLocalNPE.get());
        new Thread(()->{
            threadLocalNPE.set();
            System.out.println(threadLocalNPE.get());
        }).start();
    }
}
